'''''
import pymysql

"""连接数据库"""""

db = pymysql.connect(
    host='',
    port=3306,
    database='',
    user='root',
    password='',
    charset = 'utf8mb4'
)
cursor = db.cursor() # 创建游标对象
sql = 'select * from stu_info;'
cursor.execute(sql) # 执行sql
print(cursor.fetchall()) #查看执行结果
db.commit() # 提交
db.rollback() # 回滚
cursor.close() # 关闭游标
db.close() #关闭数据库
'''''
'''建个库，建个表，往表里插入100条数据'''

import pymysql
db = pymysql.connect(
    host='120.26.9.110',
    port=3306,
    user='root',
    password='ZA946826!',
    charset = 'utf8mb4'
)
import sqlite3
from faker import Faker

# 初始化Faker实例
fake = Faker()

# 创建数据库连接（如果数据库不存在，它会被创建）
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()

# 创建用户表
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    email TEXT NOT NULL UNIQUE,
    full_name TEXT NOT NULL,
    address TEXT,
    phone_number TEXT
);
""")

# 插入100条用户数据
'''for i in range(100):
    username = fake.user_name()
    email = fake.email()
    full_name = fake.name()
    address = fake.address()
    phone_number = fake.phone_number()
    cursor.execute("""
    INSERT INTO users (username, email, full_name, address, phone_number)
    VALUES (?, ?, ?, ?, ?)
    """, (username, email, full_name, address, phone_number))

# 提交事务
conn.commit()

# 关闭连接
conn.close()'''

import pymysql
from faker import Faker


"""连接数据库"""""

db = pymysql.connect(
    host='47.95.213.187',
    port=3306,
    user='root',
    password='Aa123456!',
    charset='utf8mb4'
)
cursor = db.cursor()
# 建库
create_database = 'create database pymysqldemo1;'
cursor.execute(create_database)

# 建表
join_database = 'use pymysqldemo1;'
cursor.execute(join_database)
create_tables= 'create table stu_info (id int(5) primary key auto_increment,name varchar(10),phone varchar(20));'
cursor.execute(create_tables)
#插入数据
fake = Faker(locale='zh_CN')


try:
    for i in range(1,101):
        name = fake.name()
        phone = fake.phone_number()
        insert_info = 'insert into stu_info values(%s,%s,%s);'
        cursor.execute(insert_info,(i,name,phone))
    db.commit()
except pymysql.Error as e:
    print(f'插入数据失败{e}')
finally:
    cursor.close()
    db.close()









import pymysql
from faker import Faker


"""连接数据库"""""

db = pymysql.connect(
    host='47.95.213.187',
    port=3306,
    user='root',
    password='Aa123456!',
    charset='utf8mb4'
)
cursor = db.cursor()
# 建库
create_database = 'create database pymysqldemo1;'
cursor.execute(create_database)

# 建表
join_database = 'use pymysqldemo1;'
cursor.execute(join_database)
create_tables= 'create table stu_info (id int(5) primary key auto_increment,name varchar(10),phone varchar(20));'
cursor.execute(create_tables)
#插入数据
fake = Faker(locale='zh_CN')


try:
    for i in range(1,101):
        name = fake.name()
        phone = fake.phone_number()
        insert_info = 'insert into stu_info values(%s,%s,%s);'
        cursor.execute(insert_info,(i,name,phone))
    db.commit()
except pymysql.Error as e:
    print(f'插入数据失败{e}')
finally:
    cursor.close()
    db.close()

